package com.eims.sls_app.ui;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.toolbox.RequestParams;
import com.eims.sls_app.dataaccess.CircleBean;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.AppTools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import bls.juejin.cn.R;

public class SelectRechargeTypeActivity extends MyBaseActivity implements
        View.OnClickListener {
    private Activity context = SelectRechargeTypeActivity.this;
    public String TAG = "SelectRechargeTypeActivity";

    private RelativeLayout layout_saoma_alipay;// 支付宝支付
    private RelativeLayout layout_zhifu_alipay;// 支付宝支付 2022年2.20
    private RelativeLayout layout_automatic_rechargey;//自动充值

    private ImageButton btn_back;// 返回


    private String opt, auth, info, time, imei, crc; // 格式化后的参数

    private MyHandler mHandler;
    private MyAsynTask mMyAsynTask;
    private CircleBean circleBean;

    private String listShowValue;


    private TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_paytype);
        mHandler = new MyHandler();
        findView();
        setListener();
        init();


        mMyAsynTask = new MyAsynTask();
        mMyAsynTask.execute();
    }

    public void findView() {


        layout_saoma_alipay = (RelativeLayout) findViewById(R.id.layout_saoma_alipay);

        btn_back = (ImageButton) findViewById(R.id.btn_back);

        tvShow = (TextView) findViewById(R.id.tv_show);
        layout_automatic_rechargey = (RelativeLayout) findViewById(R.id.layout_automatic_rechargey);
        layout_zhifu_alipay = (RelativeLayout) findViewById(R.id.layout_zifu_alipay);
    }

    public void setListener() {

        btn_back.setOnClickListener(this);

        layout_saoma_alipay.setOnClickListener(this);
        layout_automatic_rechargey.setOnClickListener(this);
        layout_zhifu_alipay.setOnClickListener(this);

    }

    public void init() {


//		String textStr = "转存1-4999元赠送<font color=\"#FF0000\">" + 100 + "%</font>红包";
//		tvShow.setText(spanned1);
        tvShow.setText(getClickableSpan());
        //此行必须有
        tvShow.setMovementMethod(LinkMovementMethod.getInstance());

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Intent intentResuls = getIntent();
        setResult(2, intentResuls);
        finish();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_back:// 返回
                finish();

                break;
            case R.id.layout_automatic_rechargey:// 自动充值
                // Intent intent1=new Intent(context,RechargeUPActivity.class);
                // context.startActivity(intent1);
                Intent intent_automatic_rechargey = new Intent();
                Bundle bundle_automatic_rechargey = new Bundle();
                intent_automatic_rechargey.setClass(SelectRechargeTypeActivity.this,
                        AutomaticRechargeActivity.class);

                intent_automatic_rechargey.putExtras(bundle_automatic_rechargey);
                startActivity(intent_automatic_rechargey);
                break;

            case R.id.layout_saoma_alipay:// 支付宝扫码支付

                Intent intent_saoma_alipay = new Intent();
                Bundle bundle_saoma_alipay = new Bundle();
                intent_saoma_alipay.setClass(SelectRechargeTypeActivity.this,
                        AmountOfCharge.class);
                bundle_saoma_alipay.putInt("Type", 1);
                intent_saoma_alipay.putExtras(bundle_saoma_alipay);
                startActivity(intent_saoma_alipay);
                break;
            case R.id.layout_zifu_alipay:// 支付宝扫码支付

                Intent intent_zifu_alipay = new Intent();
                Bundle bundle_zifu_alipay = new Bundle();
                intent_zifu_alipay.setClass(SelectRechargeTypeActivity.this,
                        AmountOfCharge.class);
                bundle_zifu_alipay.putInt("Type", 2);
                intent_zifu_alipay.putExtras(bundle_zifu_alipay);
                startActivity(intent_zifu_alipay);
                break;

        }
    }

    class MyAsynTask extends AsyncTask<Integer, Integer, String> {

        String error = "-1";

        @Override
        protected String doInBackground(Integer... params) {
            info = "";
            opt = "97";
            time = RspBodyBaseBean.getTime();
            imei = RspBodyBaseBean.getIMEI(SelectRechargeTypeActivity.this);
            // info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);

            // String key = MD5.md5(App.user.getUserPass() +
            // App.MD5_key);
            String key = App.key;

            crc = RspBodyBaseBean.getCrc(time, imei, key, info,
                    App.user.getUid());
            auth = RspBodyBaseBean.getAuth(crc, time, imei,
                    App.user.getUid());


            String[] values = {opt, auth, info};
            Log.e(TAG,
                    "晒单"
                            + RequestParams.convertParams(SelectRechargeTypeActivity.this,
                            opt, info));
            String result = HttpUtils.doPost(App.names, values,
                    App.path);
            System.out.println("晒单：" + result);


            try {
                JSONArray arr = new JSONArray(result);
                JSONObject temp = (JSONObject) arr.get(0);
                listShowValue = temp.getString("Value");
                Log.e(TAG, "晒单id" + listShowValue);


                if (listShowValue != null && !listShowValue.equals("")) {

                    error = "1";


//				    	String isShowValue=(listShowValue.split("|"))[0];
//				    	Log.i(TAG, "晒单id" + isShowValue);

                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                error = "-1";
                Log.e("x", "MyCenterActivity异常" + e.getMessage());
            }


            return error;
        }

        @Override
        protected void onPostExecute(String result) {

            mHandler.sendEmptyMessage(Integer.parseInt(result));

            super.onPostExecute(result);
        }
    }

    @SuppressLint("HandlerLeak")
    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:

                    break;
                case 1:
                    String listValue[] = new String[listShowValue.length()];
                    for (int i = 0; i < listShowValue.length(); i++) {
                        listValue[i] = listShowValue.substring(i, i + 1);
                        Log.i(TAG, "晒单id" + listValue[i]);
                    }
                    if (listValue[0].equals("1")) {
                        layout_saoma_alipay.setVisibility(View.VISIBLE);
                    } else {
                        layout_saoma_alipay.setVisibility(View.GONE);
                    }
//                    if (listValue[1].equals("1")) {
//                        layout_zhifu_alipay.setVisibility(View.VISIBLE);
//                    } else {
//                        layout_zhifu_alipay.setVisibility(View.GONE);
//                    }

                    break;


                case -1:

                    break;
                case -500:

                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private SpannableString getClickableSpan() {

        //监听器
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://m.haoqiucai.com");
                intent.setData(content_url);
                startActivity(intent);


            }
        };

        SpannableString spanableInfo = new SpannableString(Html.fromHtml(AppTools.changeStringColor("#808080",
                "温馨提示：手机触屏版支持微信和支付宝充值，")
                + AppTools.changeStringColor("#0d52c3",
                "戳我吧！")));
        int start = 21;  //超链接起始位置
        int end = 24;   //超链接结束位置


        spanableInfo.setSpan(new Clickable(listener), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spanableInfo;
    }
}

class Clickable extends ClickableSpan implements View.OnClickListener {
    private final View.OnClickListener mListener;

    public Clickable(View.OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View view) {
        mListener.onClick(view);
    }


}
